package cn.wolfcode.wolf2w.redis.service.impl;

import cn.wolfcode.wolf2w.domain.Strategy;
import cn.wolfcode.wolf2w.domain.UserInfo;
import cn.wolfcode.wolf2w.exception.LogicException;
import cn.wolfcode.wolf2w.redis.service.IStrategyStatisVORedisService;
import cn.wolfcode.wolf2w.redis.service.IUserInfoRedisService;
import cn.wolfcode.wolf2w.redis.util.RedisKeys;
import cn.wolfcode.wolf2w.redis.util.RedisValues;
import cn.wolfcode.wolf2w.redis.vo.StrategyStatisVO;
import cn.wolfcode.wolf2w.service.IStrategyService;
import cn.wolfcode.wolf2w.util.DateUtil;
import com.alibaba.fastjson.JSON;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;
import java.util.concurrent.TimeUnit;

@Service
//@Transactional
public class StrategyStatisVORedisServiceImpl implements IStrategyStatisVORedisService {

    @Autowired
    private StringRedisTemplate redisTemplate;

    @Autowired
    private IStrategyService strategyService;

    //攻略访问数自增
    @Override
    public void addSelfviewnum(String id) {
        StrategyStatisVO vo = getStrategyStatisVO(id);
        //修改值
        vo.setViewnum(vo.getViewnum()+1);
        this.setStrategyStatisVO(vo);
    }

    //攻略评论数自增
    @Override
    public void addReplynum(String strategyId) {
        StrategyStatisVO vo = getStrategyStatisVO(strategyId);
        vo.setReplynum(vo.getReplynum()+1);
        this.setStrategyStatisVO(vo);
    }

    //收藏功能
    @Override
    public boolean strategyFavorStatis(String sid, String uid) {
        //获取key
        String uidKey = RedisKeys.USER_FAVOR.join(uid);
        //获取vo对象
        StrategyStatisVO vo = this.getStrategyStatisVO(sid);
        List<String> sidList=new ArrayList<>();
        //判断redis是否存在uidKey
        if (redisTemplate.hasKey(uidKey)) {
            //存在,表示取消收藏操作
            String sidStr = redisTemplate.opsForValue().get(uidKey);
            //转换格式
            sidList=JSON.parseArray(sidStr,String.class);
        }
        //判断是否包含sid
        if (sidList.contains(sid)) {
            //包含,取消收藏操作
            vo.setFavornum(vo.getFavornum()-1);
            sidList.remove(sid);
        } else {
            //不包含,收藏操作
            vo.setFavornum(vo.getFavornum()+1);
            sidList.add(sid);
        }
        this.setStrategyStatisVO(vo);
        //更新操作
        redisTemplate.opsForValue().set(uidKey,JSON.toJSONString(sidList));
        return sidList.contains(sid);
    }

    //顶功能(点赞功能)
    @Override
    public boolean strategyThumbsupStatis(String sid,String uid) {
        //获取key
        String key = RedisKeys.STRATEGY_THUMBSUP.join(uid, sid);
        //判断redis是否有key
        if (redisTemplate.hasKey(key)) {
            //包含当天已经顶过,返回false
            return false;
        }
        //不包含,当天没顶过,点赞数+1
        StrategyStatisVO vo = this.getStrategyStatisVO(sid);
        vo.setThumbsupnum(vo.getThumbsupnum()+1);
        this.setStrategyStatisVO(vo);
        //设置标识有效时间
        //开始时间
        Date now = new Date();
        //结束时间
        Date end = DateUtil.getEndDate(now);
        //调用方法获取开始时间到结束时间的间隔(单位:秒)
        //也就是该标识有效时间
        long time = DateUtil.getDateBetween(now, end);
        //存入标识
        //获取value
        String value = RedisValues.STRATEGY_THUMBSUP.join(sid);
        redisTemplate.opsForValue().set(key,value,time,TimeUnit.SECONDS);
        return true;

    }

    //获取指定攻略的vo对象
    @Override
    public StrategyStatisVO getStrategyStatisVO(String id) {
        //获取key
        String key = RedisKeys.STRATEGY_STATIS_VO.join(id);
        StrategyStatisVO vo=null;
        //判断redis里面是否存在该key
        if (redisTemplate.hasKey(key)) {
            //存在,直接使用
            String voStr = redisTemplate.opsForValue().get(key);
            vo = JSON.parseObject(voStr, StrategyStatisVO.class);
        } else {
            //没有,创建存进redis中
            vo = new StrategyStatisVO();
            Strategy strategy = strategyService.findByOne(id);
            //赋值操作
            BeanUtils.copyProperties(strategy,vo);
            vo.setStrategyId(strategy.getId());
        }
        return vo;
    }

    //根据id设置指定攻略vo值
    @Override
    public void setStrategyStatisVO(StrategyStatisVO vo) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(vo.getStrategyId());
        redisTemplate.opsForValue().set(key,JSON.toJSONString(vo));
    }

    @Override
    public boolean isStrategyVOExists(String sId) {
        String key = RedisKeys.STRATEGY_STATIS_VO.join(sId);
        return redisTemplate.hasKey(key);
    }

    //查询匹配的vo对象(Redis)
    @Override
    public List<StrategyStatisVO> queryStrategyVOListByParameter(String parameter) {
        List<StrategyStatisVO> list = new ArrayList<>();
        //获取key
        String queryKey = RedisKeys.STRATEGY_STATIS_VO.join(parameter);
        //查询数据库(获取匹配的key)
        Set<String> keys = redisTemplate.keys(parameter);
        //判断集合对象不等于空并且集合长度大于0在进行封装操作
        if (keys!=null&&keys.size()>0) {
            //遍历所有key
            for (String key : keys) {
                String queryVOStr = redisTemplate.opsForValue().get(key);
                list.add(JSON.parseObject(queryVOStr,StrategyStatisVO.class));
            }
        }
        return list;
    }
}
